
/**
 * 创建矩形entity
 */

import Entity from "../Entity"

export default class Rectangle extends Entity {

  constructor(positions, options = {}) {
    super()
    this._positions = positions
    this._type = 'rectangle'
    this._style = {
      ...options
    }

    this.delegate = new Cesium.Entity({ rectangle: {} })
    this.mounted()
  }

  /**
   * 初始化对象属性
   */

  mounted() {
    this.style = this._style
    this.positions = this._positions
  }

  /** 
  * @param {*} style Rectangle的样式
  * @returns 
  */

  /**
    * 旋转
    * @param {*} angle 
    */

  startRotate(angle = 1) {
    this.pitch = angle
    this.delegate.rectangle.rotation = new Cesium.CallbackProperty((time, result) => {
      this.rotateAmount += this.pitch
      if (this.rotateAmount > 360 || this.rotateAmount < -360) {
        this.rotateAmount = 0
      }
      this.heading = this.rotateAmount * Math.PI / 180
      return this.heading
    })
  }

  /**
   * 停止旋转
   */

  stopRotate() {
    this.delegate.rectangle.rotation = new Cesium.CallbackProperty((time, result) => {
      return this.heading
    })
  }

  setStyle(style) {
    if (!style) return
    Object.assign(this._style , {...style})
    Object.assign(this.delegate.rectangle, { ...this._style })
  }

  get style (){
    return this._style
  }

  set style(val){
    Object.assign(this._style , {...val})
    Object.assign(this.delegate.rectangle , {...this._style})
  }

  get positions() {
    return this._positions
  }

  set positions(val) {
    this._positions = val
    this.delegate.rectangle.coordinates = Cesium.Rectangle.fromCartesianArray(this._positions)
  }


}